package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.mapper.CheckitemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckgroupService;
import com.itheima.service.CheckitemService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CheckgroupServiceImpl extends ServiceImpl<CheckgroupMapper, CheckGroup> implements CheckgroupService {
    @Override
    public void addGroup(CheckGroup checkGroup, Integer[] checkitemIds) {
        Integer groupId = checkGroup.getId();
        if (groupId!=null){
            baseMapper.deleteCheckitemsByGroupId(groupId);
        }
        // saveOrUpdate 此方法主键值为null或者为0为插入,否则为更新 且会自动获取主键值
        saveOrUpdate(checkGroup);
        groupId = checkGroup.getId();
        if (checkitemIds!=null){
            for (Integer checkitemId : checkitemIds) {
                baseMapper.addCheckgroupWithCHeckitem(groupId,checkitemId);
            }
        }
    }

//    @Override
//      当查询语句中有or的时候 要特别注意 or会将前后条件分成两部分完全独立
//    public Map findPage(QueryPageBean queryPageBean) {
//        // 这里要写泛型的
//        LambdaQueryWrapper<CheckGroup> lambda = new QueryWrapper<CheckGroup>().lambda();
//        String queryString = queryPageBean.getQueryString();
//        queryString=queryString==null?"":queryString;
//        lambda.like(CheckGroup::getName,queryString).or().like(CheckGroup::getHelpCode,queryString);
//        // 对于这个业务来说,上下这两行代码必须是这样的顺序 逻辑上才不会出错
//        lambda.eq(CheckGroup::getIs_delete, 0);
//
//        // 此方法用于分页查询 总记录数与结果集都已封装至结果
//        Page<CheckGroup> page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), lambda);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("total",page.getTotal());
//        map.put("list",page.getRecords());
//        return map;
//    }

    public Map findPage(QueryPageBean queryPageBean) {

        String queryString = queryPageBean.getQueryString();
        queryString=queryString==null?"":queryString;

        // 查询总记录数
        Integer count = baseMapper.findCount(queryString);

        int start = (queryPageBean.getCurrentPage()-1)*queryPageBean.getPageSize();
        List<CheckGroup> list = baseMapper.findGroups(queryString, start, queryPageBean.getPageSize());
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",count);
        map.put("list",list);
        return map;
    }




    public List<Integer> findCheckitemIds(Integer groupId) {
        List<Integer> checkitemIds = baseMapper.findCheckitemIds(groupId);
        return checkitemIds;
    }

    @Override
    public List<CheckGroup> findAll() {
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<CheckGroup> list = list(queryWrapper);
        return list;
    }
}


